package com.buss.controller;

import com.buss.model.UploadFileLine;
import com.buss.model.UploadFileType;
import com.buss.util.Constant;
import com.buss.util.map.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class ManageSystemController extends BaseFileController {

    /**
     * RESTful模式 调用
     */
    @GetMapping("/{MODULECODE}/{FILETYPECODE}")
    @ResponseBody
    public Map<String, Object> handleFileUpload (@PathVariable String MODULECODE,
                                                 @PathVariable String FILETYPECODE,
                                                 HttpServletRequest request) {
        Map<String, Object> response = new HashMap<String,Object>();
        response.put("MODULECODE", MODULECODE);
        response.put("FILETYPECODE", FILETYPECODE);
        return response;
    }

    @GetMapping("/get/{key}")
    @ResponseBody
    public Map<String, Object> get (@PathVariable String key,
                                    UploadFileType uploadFileTypeForm,
                                    UploadFileLine uploadFileLineForm) {
        Map<String, Object> response = new HashMap<String,Object>();
        if("".equals(key)){
            response.put(Constant.COMMON_RETURN_KEY, new Result().inputParamIsNullError());
            return response;
        }

        if("type".equals(key)){
            // 查文件类型表
            List<UploadFileType> listUploadFileType = uploadFileTypeService.select(uploadFileTypeForm);
            response.put("datalist", listUploadFileType);
            int total = uploadFileTypeService.selectCount(uploadFileTypeForm);
            response.put("total", total);
            response.put(Constant.COMMON_RETURN_KEY, new Result().success());
            return response;
        }
        if("line".equals(key)){
            // 查上传流水表
            List<UploadFileLine> listUploadFileLine = service.select(uploadFileLineForm);
            response.put("datalist", listUploadFileLine);
            int total = service.selectCount(uploadFileLineForm);
            response.put("total", total);
            response.put(Constant.COMMON_RETURN_KEY, new Result().success());
            return response;
        }
        response.put(Constant.COMMON_RETURN_KEY, new Result().addErrorMsg("Parameter error, No result!"));
        return response;
    }


}
